//
//  HotTopicSubSegViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/31.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "HotTopicNaviView.h"
#import "CommonThemeModel.h"

#import "XLPageViewController.h"
#import "HotTopicSubListViewController.h"

#import "HotTopicSubSegViewController.h"

@interface HotTopicSubSegViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) UIImageView *headImageView;

@property (nonatomic, strong) HotTopicNaviView *naviView;

@property (nonatomic, strong) NSMutableArray <CommonThemeModel *>*dataArray;

@end

@implementation HotTopicSubSegViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self request_themeCat];
    [self initView];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)initView {

    self.view.backgroundColor = UIColorHex(0xff4242);
    
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif

    [self.view addSubview:self.headImageView];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(100);
    }];

    self.headImageView.backgroundColor = UIColorHex(0xff4242);

    [self.view addSubview:self.naviView];
    
    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(kStatusBarHeight + NavBar_Height);
    }];
}

- (void)goBack {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initPageViewController {
    
    XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
    config.shadowLineColor = UIColorHex(0xffffff);
    config.titleNormalColor = UIColorHex(0xf1f1f1);
    config.titleSelectedColor = UIColorHex(0xffffff);
    config.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
    config.titleNormalFont = [UIFont systemFontOfSize:16];
    config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
    config.showTitleInNavigationBar = false;
    config.separatorLineHidden = true;
    config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
    config.shadowLineWidth = 20;
    
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];

    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(100);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
//        make.height.mas_equalTo(NavBar_Height);
    }];
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {

    HotTopicSubListViewController *vc = [[HotTopicSubListViewController alloc] initWithSitId:self.dataArray[index].sitId];
    return vc;
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.dataArray[index].sitName;
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.dataArray.count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    
//    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[index].sitImg]];
    self.headImageView.backgroundColor = UIColorHex(0xff4242);
}


- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        _headImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _headImageView;
}

- (HotTopicNaviView *)naviView {
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:@"HotTopicNaviView"owner:nil options:nil].firstObject;
        _naviView.titleLab.text = @"热门主题";
        [_naviView.backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _naviView;
}

- (NSMutableArray<CommonThemeModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)request_themeCat {

    NSMutableDictionary *dic = @{}.mutableCopy;

    [dic setValue:self.classId forKey:@"goodsType"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_AllTheme];

    [SVProgressHUD showWithStatus:@"正在加载"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                NSArray *resultArray = [data valueForKey:@"data"];

                weakSelf.dataArray = @[].mutableCopy;
                if ([resultArray isKindOfClass:[NSArray class]]) {
                    
                    
                    for (int i = 0; i < resultArray.count; i++) {
                        NSDictionary *resultArrayDic = resultArray[i];
                        CommonThemeModel *model = [CommonThemeModel modelWithDictionary:resultArrayDic];
                        
                        if (i==0) {
//                            [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:model.sitImg]];
                        }
                        [weakSelf.dataArray addObject:model];
                    }
                }

                if (weakSelf.dataArray.count > 0) {
                    [weakSelf initPageViewController];
                } else {

                }
            }
        }

    } FailedBlock:^(NSError * _Nonnull error) {

        [SVProgressHUD dismiss];
    }];

}

@end
